package com.sugar.psych.assessment.module.business.controller.app.user;

import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.module.business.controller.admin.school.vo.SchoolRespVO;
import com.sugar.psych.assessment.module.business.controller.app.user.vo.AppBizUserRespVO;
import com.sugar.psych.assessment.module.business.controller.app.user.vo.StudentAuthReqVO;
import com.sugar.psych.assessment.module.business.controller.app.user.vo.UserUpdateReqVO;
import com.sugar.psych.assessment.module.business.controller.app.user.vo.WxRegisterReqVO;
import com.sugar.psych.assessment.module.business.service.school.SchoolService;
import com.sugar.psych.assessment.module.business.service.user.UserService;
import com.sugar.psych.assessment.module.system.api.auth.dto.AppAuthDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.util.List;

import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.successApp;
import static com.sugar.psych.assessment.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @author Y.sugar
 * @since 2025/7/1 17:13
 */
@Tag(name = "APP - 用户信息")
@RestController
@RequestMapping("/psych/user")
@Validated
public class AppBizUserController {
    @Resource
    private UserService userService;
    @Resource
    private SchoolService schoolService;

    @PostMapping("/login/mobile")
    @Operation(summary = "微信 - 根据微信手机号code登录/注册")
    @PermitAll
    public CommonResult<AppAuthDTO> loginByMobileCode(@RequestBody WxRegisterReqVO reqVO) {
        return successApp(userService.loginByMobileCode(reqVO.getCode()));
    }

    @PostMapping("/auth/student")
    @Operation(summary = "学生认证")
    public CommonResult<Boolean> studentAuth(@Valid @RequestBody StudentAuthReqVO reqVO) {
        userService.studentAuth(reqVO, getLoginUserId());
        return successApp(true);
    }

    @GetMapping("/get/school")
    @Operation(summary = "获取当前学校")
    public CommonResult<List<SchoolRespVO>> getSchool() {
        return successApp(schoolService.getAllSchool());
    }

    @GetMapping
    @Operation(summary = "获取当前用户信息")
    public CommonResult<AppBizUserRespVO> getNowLoginUserINfo() {
        return successApp(userService.getBizUser(getLoginUserId()));
    }

    @PutMapping
    @Operation(summary = "修改个人信息")
    public CommonResult<Boolean> updateUser(@RequestBody UserUpdateReqVO reqVO) {
        userService.appUpdateUser(getLoginUserId(), reqVO);
        return successApp(true);
    }
}
